<?php

namespace Index\Controller;

class IndexController extends CommonController {


    //首页
    public function index(){
        $Books = D('Common/Book');
        //首页推荐展示
        $ids = \Common\Model\SettingModel::getValue('index-tj');
        $tjbook = $Books->getList("book_id IN ({$ids})",'',0,6,'',false);
        $this->assign('tjbook',$tjbook['lists']);
        //首页强推
        $idsa = \Common\Model\SettingModel::getValue('jind-tj');
        $jindTj = $Books->getList("book_id IN ({$idsa})",'',0,26,'cate_id,book_name,book_id,author_id',false);
        $this->assign('jindTj',$jindTj['lists']);



        //网站公告
        $this->assign('gonggao',D('Common/notice')->where('`disabled`=1')->order('ctime desc')->select());

        //最新入库
        $newBook = $Books->getList('','create_time DESC,book_id DESC',0,29,'',false);
        $this->assign('newBook',$newBook['lists']);

        //最新更新
        $Article = D('Common/BookArticle');
        $Article->_useCache = false;
        $newList = $Article->getList('','create_time DESC,article_id DESC',0,29,'',false);
        $this->assign('newList',$newList['lists']);

        $this->display();
    }


    //登陆
    public function DOlogin(){
        C('LAYOUT_ON',false);
        if(!$this->checkVerify($_POST['verify'])){
            $this->error("验证码错误");exit;
        }

        if(empty($_POST['username']) || empty($_POST['__hash__']) || empty($_POST['password'])){
            $this->display();exit;
        }
        $username = I('post.username');
        $pass = I('post.password');
        $Pass = new \Common\Model\MemberPamModel();
        if($Pass->autoCheckToken($_POST)){
            $info = $Pass->login($username);
            $pass = genSecretStr($pass,$info['password_account']);
            if($info['login_password'] === $pass){
                $minfo = \Common\Library\COM\MemberDo::getLoginInfo($info['member_id'],'login');
                session('_id',$minfo['member_id']);
                session('_info',$minfo);
                $y = date('Y',$minfo['last_login']);
                $m = date('m',$minfo['last_login']);
                $d = date('d',$minfo['last_login']);
                if(date('Y',time()) > $y || date('m',time()) > $m || date('d',time()) > $d){
                    \Common\Library\COM\MemberDo::addMemberPoint($info['member_id'],10,'每天登录奖励10个积分');
                }
                $this->success('登录成功','/');exit;
            }
        }
        $this->error('用户名或密码错误');
    }


    //注册
    public function DOregist(){
        if(!$this->checkVerify($_POST['verify'])){
            $this->error("验证码错误");exit;
        }
        $login_account = I('post.username');
        $password = I('post.password');
        if(isUsername($login_account)){
            $Members = new \Common\Model\MembersModel();
            if(!$Members->autoCheckToken($_POST)){
                $this->error('注册失败');exit;
            }
            if(\Common\Library\COM\MemberDo::checkUsername($login_account)){
                $this->error('该用户名:'.$username.' 已注册');exit;
            }
            $data = array('regtime'=>  time(),'reg_ip'=>  get_client_ip(),'last_login'=>  time());
            $Members = new \Common\Model\MembersModel();
            if($member_id = $Members->add($data)){
                $Pass = new \Common\Model\MemberPamModel();
                $password_account = getRandStr(10);
                $login_password = genSecretStr($password,$password_account);
                $data = array(
                    'member_id'=>$member_id,
                    'login_account'=>$login_account,
                    'login_password'=>$login_password,
                    'password_account'=>$password_account,
                );
                if($Pass->add($data)){
                    session('_id',$member_id);
                    session('_info',\Common\Library\COM\MemberDo::getLoginInfo($member_id,'login'));
                    \Common\Library\COM\MemberDo::addMemberPoint($member_id,10,'每天登录奖励10个积分');
                    $this->success('注册成功');exit;
                }
            }
        }
        $this->error('注册信息有误');
    }

    //忘记密码
    public function forgetPassword(){
        C('LAYOUT_ON',false);
        $this->display();
    }


    public function login(){
        if($this->_id > 0){
            $this->redirect('Index/index');exit;
        }
        C('LAYOUT_ON',false);
        $this->display();
    }

    public function regist(){
        if($this->_id > 0){
            $this->redirect('Index/index');exit;
        }
        C('LAYOUT_ON',false);
        $this->display();
    }

    //搜索
    public function Search(){
        $search = I('post.search');
        $search = trim($search);
        if(empty($search)){
            $this->layerIfreamRe('检索内容不能为空','','error');
        }
        if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$search)){
            $this->layerIfreamRe('搜索的关键字不合法',C('SYS_SITE'),'error');
        }
        $Book = new \Common\Model\BookModel();
        if($Book->autoCheckToken($_POST)){
            $books = $Book->where("`book_name` like '{$search}%'")->select();
            $this->assign('books',$books);
            $this->display();
            exit;
        }
        $this->layerIfreamRe('页面不存在!','','error');
    }

    //排行榜
    public function newph(){
        $brows = D('Common/Book')->getList('','brow_num DESC',0,15);
        $this->assign('brows',$brows['lists']);
        $this->display('Index/newph');
    }

    //等级说明
    public function lvList(){
        $Lv = new \Common\Model\MemberLvModel();
        $this->assign('lvlist',$Lv->where('`disabled`=1')->order('point asc')->select());
        $this->display();
    }

    public function all(){
        $books = M()->query("select au.name as author_name,book_id,book_name from sdb_book_author as au,sdb_book as bo where bo.author_id = au.author_id");
        $this->assign('books',$books);
        $this->display('all');
    }

    //阅读记录
    public function readLog(){
        $log = json_decode(cookie(),true);
        //p((cookie()));exit;

        $this->display('readlog');
    }
    public function getConfig(){
        $Books = D('Common/Book');
        $newBook = $Books->getList('','create_time DESC,book_id DESC',0,29,'',false);
        p($newBook);exit;
        p(C());

        p(C('HTML_PATH'));
    }


}